package org.cqepc.project.reform.utils.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class Result<T> {

    @Schema(description = "状态码", example = "20000")
    private Integer code; //状态码

    @Schema(description = "状态信息（成功 失败）")
    private String message; //返回状态信息（成功 失败）

    @Schema(description = "数据", subTypes = {})
    private T data; //返回数据

    public Result() {
    }

    //成功的方法，有data数据
    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(20000);
        result.setMessage("成功");
        return result;
    }

    //失败的方法,有data数据
    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(20001);
        result.setMessage("失败");
        return result;
    }

    public Result<T> message(String msg) {
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}
